<template>
	<view class="wrap">
		<!-- 头部搜索 -->
		<view class="wrap__header">
			 <uni-icons type="left" color="#fff" size="26"  @click="backClick"></uni-icons>
			<view class="header__search">
				<image src="@/static/icons/search.png" mode="widthFix" class="search__icon"></image>
				<input type="text" v-model.trim="searchVal" :placeholder="$t('search.placeHolder')" maxlength="20" class="search__input"/>
			</view>
			<text class="header__title" @click="searchClick">{{$t("search.btn")}}</text>
		</view>
		<!-- 搜索歷史 -->
		<searchHistory v-if="isShowHistory" :historyList="historyList" @searchHandle="searchHandle" @deleteHandle="deleteHandle">
		</searchHistory>
		<!-- 搜索结果 -->
		<scroll-view scroll-y="true" class="wrap__result">
			<view class="result__list" v-if="searchResult.length">
				<view class="list__item" v-for="(item, index) in searchResult" :key="index" @click="stockClick(item)">
					<view class="item__title">{{item.name}}</view>
					<view class="item__info">
						<image v-if="item.shichang == 'Japan' || item.turnover_section == 'JP'" src="@/static/icons/KRN.png" mode="widthFix"
							class="info__icon"></image>
						<image v-if="item.shichang == 'USA' || item.turnover_section == 'US'" src="@/static/icons/US.png" mode="widthFix"   
							class="info__icon"></image>
						<image v-if="item.shichang == 'UK' || item.turnover_section == 'UK'" src="@/static/icons/UK.png" mode="widthFix"
							class="info__icon"></image>
						<!-- <text class="info__tag">{{item.zimudaima?item.zimudaima.split(":")[0]:""}}</text> -->
						<text class="info__num">{{item.shuzidaima}}</text>
					</view>
				</view>
			</view>
			<!-- 無數據 -->
			<!-- <emptyTips v-else style="margin-top: 300rpx;"></emptyTips> -->
		</scroll-view>
	</view>
</template>

<script>
	import {getUrlParams} from '@/common/js/utils.js'
	import searchHistory from '@/components/search/searchHistory.vue'
	import emptyTips from '@/components/common/emptyTips.vue'
	import {mapState} from 'vuex'
	export default {
		components: {
			searchHistory,
			emptyTips
		},
		data() {
			return {
				searchVal:"", //搜索代码
				searchResult:[], //搜索结果
				historyList:[], //搜索历史
				isShowHistory:false, //是否显示搜索历史
			}
		},
		onLoad() {
			let searchHistory = uni.getStorageSync("searchHistory")
			if(searchHistory){
				this.isShowHistory = true
				this.historyList = searchHistory
			}
		},
		watch:{
			searchVal(value){
				if(value===""){
					this.isShowHistory = true
					this.searchResult = []
				}
			}
		},
		methods: {
			// 删除记录处理
			deleteHandle(index){
				if(index >= 0){
					this.historyList.splice(index, 1)
				}else{
					this.historyList = 0
				}
				uni.setStorageSync("searchHistory", this.historyList)
			},
			// 搜索点击
			async searchClick(){
				let {searchVal, historyList} = this
				if(!searchVal){
					uni.showToast({
						title:"please input",
						icon:"none"
					})
					return
				}
				uni.showLoading({
					title:this.$t('loading.loading'),
					mask:true
				})
				if(!historyList.includes(searchVal)){ //添加搜索记录
					historyList.unshift(searchVal)
					this.historyList = historyList.slice(0, 10)
					uni.setStorageSync("searchHistory", this.historyList)
				}
				this.searchResult = []
				
				
				let res = await this.$api.search(searchVal)
				uni.hideLoading()
				if(res.code === 1 && res.data){
					if(res.data){
						this.isShowHistory = false
						this.searchResult = res.data
					}else{
						this.isShowHistory = true
						uni.showToast({
							title:"no result",
							icon:"none"
						})
					}
				}else{
					uni.showToast({
						title:res.msg,
						icon:"none"
					})
				}
				console.log(this.searchResult)
				if(!this.searchResult){
					
				
			
			    uni.request({
				    url: 'https://api.investing.com/api/search/v2/search?q='+searchVal,
					method: 'GET',
				    data: {},
				    success: (res) => {
				       this.isShowHistory = false
						let list = res.data.quotes
						
						uni.hideLoading()
						let  shuju = [];
						
						list.map(item=>{
						    item.shuzidaima = item.symbol
						    item.name = item.description
						    item.shichang = item.flag
							//flag   USA   Japan   UK
							
							
							if(item.flag == 'USA' || item.flag == 'Japan' || item.flag == 'UK'){
								if(item.symbol.toUpperCase().toLowerCase() == searchVal.toUpperCase().toLowerCase() ){
									shuju.push(item)
																		//把数据传到后台
																		let  param = {
																			"id" :item.id,
																			"classi_id" :item.class_id,
																			"flag" : item.flag,
																			"high" : 0,
																			"low" : 0,
																			"name" : item.description,
																			"open" : 0,
																			"price" : 0,
																			"shuzidaima" : item.symbol,
																			"turnover" : 0,
																			"zhangdiebaifenbi" : 0,
																			"zhangdieshu" : 0,
																			"zimudaima" : item.symbol,
																			"zongshizhi" : 0
																		};
																		
																		
																		
																	    let res =   this.$api.chuli(param)

								}
							}
							
						
						})
						
						this.searchResult  = shuju
					
						
						
						
						
				    },
				    fail: (err) => {
				        console.error(err); // 请求失败的错误信息
				    }
				});
				
				}
				
				
				// let res = await this.$api.queryStockCode(searchVal)
				// uni.hideLoading()
				// if(res.code === 1 && res.data){
				// 	if(res.data?.data){
				// 		this.isShowHistory = false
				// 		this.searchResult = res.data.data
				// 	}else{
				// 		this.isShowHistory = true
				// 		uni.showToast({
				// 			title:"no result",
				// 			icon:"none"
				// 		})
				// 	}
				// }else{
				// 	uni.showToast({
				// 		title:res.msg,
				// 		icon:"none"
				// 	})
				// }
			},
			// 搜索历史点击搜索
			searchHandle(e){
				this.searchVal = e
				this.searchClick()
			},
			backClick(){
				let canNavBack = getCurrentPages()
				if( canNavBack && canNavBack.length>1) {  
					uni.navigateBack() 
				} else {  
				    history.back();  
				}
			},
			// 
			stockClick(item){
				uni.navigateTo({
					url:`/pages/account/stockDetail?id=${item.id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.wrap {
		min-height: 100vh;
		padding: 0 30rpx;
		padding-top: 50rpx;
		box-sizing: border-box;
		.wrap__header{
			@include flexRowAlign(space-between);
			.header__title{
				font-size: 32rpx;
				font-weight: 800;
				color: #FFFFFF;
				width: 120rpx;
				height: 72rpx;
				background: #458EF4;
				border-radius: 0rpx 8rpx 8rpx 0rpx;
				@include flexCenter();
			}
			.header__search{
				@include flexRowAlign();
				flex: 1;
				height: 72rpx;
				background: #2E303D;
				border-radius: 8rpx 0 0 8rpx;
				padding-left: 30rpx;
				box-sizing: border-box;
				margin-left: 20rpx;
				.search__icon{
					width: 36rpx;
					height: 36rpx;
				}
				.search__input{
					font-size: 30rpx;
					color: #fff;
					padding-left: 24rpx;
					box-sizing: border-box;
				}
			}
		}
		.wrap__result{
			margin-top: 24rpx;
			.result__list{
				.list__item{
					height: 120rpx;
					background: #161827;
					border-top: 1rpx solid #EEEEEE;
					padding: 24rpx 0;
					box-sizing: border-box;
					.item__title{
						font-size: 28rpx;
						color: #fff;
						    overflow: hidden;
						    white-space: nowrap;
						    text-overflow: ellipsis;
					}
					.item__info{
						@include flexRowAlign();
						margin-top: 10rpx;
						.info__tag{
							padding: 2rpx 8rpx;
							height: 32rpx;
							background: #DE2804;
							border-radius: 6rpx;
							font-size: 24rpx;
							font-weight: 800;
							color: #161827;
						}
						.info__num{
							font-size: 24rpx;
							color: #fff;
							margin-left: 10rpx;
						}
						.info__icon{
							width: 30rpx;
							height: 30rpx;
							margin-right: 10rpx;
							border-radius: 50%;
						}
					}
				}
			}
		}
	}
</style>